import Vue from 'vue'
import Vuex from 'vuex'

import request from '../utils/request'
import user from './user'
import cart from './cart'
import address from './address'

Vue.use(Vuex)

const store = new Vuex.Store({
    modules: {
        user,
        cart,
        address
    },
    actions: {
        async tokenCheck(context) {
            const { data } = await request.post('/tokenCheck', {
                username: context.state.user.userInfo.username,
            },
                {
                    headers: { token: context.state.user.userInfo.token },
                })

            return data;

            //使用：在组件中直接 const data= this.$store.dispatch('tokenCheck') 返回值为一个preomise对象
        }

    },
    mutations: {
        logout(state) {
            state.user.userInfo = {}
            state.cart.cartlist = []
            state.address.address = {}
            localStorage.removeItem('cartlist')
            localStorage.removeItem('userInfo')
            localStorage.removeItem('address')
        }
    }



})




export default store;